package com.csun.dsms.task;

import com.csun.dsms.mapper.HgReminderConfigMapper;
import com.csun.dsms.mapper.IhgUserMapper;
import com.csun.dsms.model.dto.yc.YcRequestBean;
import com.csun.dsms.model.huang.HgReminderConfig;
import com.csun.dsms.model.huang.IhgUser;
import com.csun.dsms.service.HgReminderDataService;
import com.csun.dsms.utils.WorkingDaysCalculator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@Component
@EnableScheduling
public class IHgUploadTask {

    @Resource
    private IhgUserMapper ihgUserMapper;



    // 每天执行一次
    // 获取当前day;
    //0 15 10 ? * *
    @Scheduled(cron = "0 0 01 * * ?")
    public void addRemind(){
        // 获取当前day;
        LocalDate today = LocalDate.now();
        int dayOfMonth = today.getDayOfMonth();
        // 本月第一天
        if (dayOfMonth == 1){
            setAllEnableUpload();
        }else if (dayOfMonth>5 && dayOfMonth <15){
            // 获取当前
            int year = today.getYear();
            int month = today.getMonthValue()+1;
            LocalDate startDate = LocalDate.of(year, month, 1);
            int workDays = WorkingDaysCalculator.calculateWorkDays(startDate, today);
            if (workDays>5){
                setAllDisableUpload();
            }
        }
    }



    private void setAllDisableUpload() {
        List<IhgUser> allUserList = ihgUserMapper.selectAll();
        allUserList.forEach(item ->{
            item.setIsUpload(0);
            ihgUserMapper.updateByPrimaryKey(item);
        });
    }

    private void setAllEnableUpload() {
        List<IhgUser> allUserList = ihgUserMapper.selectAll();
        allUserList.forEach(item ->{
            item.setIsUpload(1);
            ihgUserMapper.updateByPrimaryKey(item);
        });
    }



}
